//
//  reviseMiMaViewController.m
//  Model
//
//  Created by apple on 2018/7/9.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "reviseMiMaViewController.h"

@interface reviseMiMaViewController ()
@property (nonatomic, strong) UITextField *zhangHaoTF;
@property (nonatomic, strong) UITextField *miMaTF;
@property (nonatomic, strong) UITextField *yanZhengMaTF;
@property (nonatomic, strong) UIButton *yzmBtn;
@end

@implementation reviseMiMaViewController
//电池颜色
- (UIStatusBarStyle)preferredStatusBarStyle{
    
    return UIStatusBarStyleLightContent;
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creationView];
    
    // Do any additional setup after loading the view.
}
- (void)creationView{
    
    UIImageView *beiJingImgView = [[UIImageView alloc] init];
    UIImage *beiJingImg = [UIImage imageNamed:@"bg_head_logo"];
    [self.view addSubview:beiJingImgView];
    [beiJingImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.offset(beiJingImg.size.height);
        
    }];
    beiJingImgView.userInteractionEnabled = YES;
    
    beiJingImgView.image =beiJingImg;
    UIButton *backButton = [[UIButton alloc] init];
    [beiJingImgView addSubview:backButton];
    [backButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(38);
        make.left.equalTo(self.view).with.offset(13);
        make.width.offset(screenScale(25));
        make.height.offset(screenScale(25));
    }];
    [backButton addTarget:self action:@selector(leftNavItemClick) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLab = [[UILabel alloc] init];
    [beiJingImgView addSubview:titleLab];
    titleLab.text = @"修改密码";
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(backButton);
    }];
    titleLab.textColor = [UIColor whiteColor];
    titleLab.font = [UIFont systemFontOfSize:17];
    
//    UIImageView *logoImgView = [[UIImageView alloc] init];
//    [self.view addSubview:logoImgView];
//    logoImgView.image = [UIImage imageNamed:@"logo_z_y"];
//    [logoImgView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(titleLab.mas_bottom).with.offset(80);
//        make.centerX.equalTo(self.view);
//        make.width.offset(122);
//        make.height.offset(122);
//
//    }];
    
    UIImageView *zhangHaoImgView = [[UIImageView alloc] init];
    [self.view addSubview:zhangHaoImgView];
    [zhangHaoImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(beiJingImgView.mas_bottom).with.offset(13);
        make.left.equalTo(self.view).with.offset(38);
        make.right.equalTo(self.view).with.offset(-38);
        make.height.offset(44);
    }];
    
    zhangHaoImgView.image = [UIImage imageNamed:@"line_frame"];
    zhangHaoImgView.userInteractionEnabled = YES;
    
    UIImageView *zhangHaoIcon = [[UIImageView alloc] init];
    [zhangHaoImgView addSubview:zhangHaoIcon];
    UIImage *zhangHaoIconImg =[UIImage imageNamed:@"icon_phone"];
    [zhangHaoIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(zhangHaoImgView).with.offset(16);
        make.centerY.equalTo(zhangHaoImgView);
        make.width.offset(zhangHaoIconImg.size.width);
        make.height.offset(zhangHaoIconImg.size.height);
    }];
    zhangHaoIcon.image = [UIImage imageNamed:@"icon_phone"];
    
    UIView *zhangHaoLine = [[UIView alloc] init];
    [zhangHaoImgView addSubview:zhangHaoLine];
    [zhangHaoLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(zhangHaoIcon.mas_right).with.offset(8);
        make.height.offset(14);
        make.width.offset(1);
        make.centerY.equalTo(zhangHaoIcon);
        
    }];
    zhangHaoLine.backgroundColor = getColor(@"ff40c6");
    
    self.zhangHaoTF = [[UITextField alloc] init];
    [zhangHaoImgView addSubview:self.zhangHaoTF];
    [self.zhangHaoTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(zhangHaoIcon.mas_right).with.offset(17);
        make.right.equalTo(zhangHaoImgView).with.offset(-16);
        make.centerY.equalTo(zhangHaoIcon);
    }];
    self.zhangHaoTF.placeholder = @"请输入账号";
    self.zhangHaoTF.font = [UIFont systemFontOfSize:14];
    self.zhangHaoTF.textColor = getColor(@"ff40c6");
    //密码
    UIImageView *miMaImgView = [[UIImageView alloc] init];
    [self.view addSubview:miMaImgView];
    [miMaImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(zhangHaoImgView.mas_bottom).with.offset(16);
        make.left.equalTo(self.view).with.offset(38);
        make.right.equalTo(self.view).with.offset(-38);
        make.height.offset(44);
    }];
    
    miMaImgView.image = [UIImage imageNamed:@"line_frame"];
    miMaImgView.userInteractionEnabled = YES;
    
    UIImageView *miMaIcon = [[UIImageView alloc] init];
    [miMaImgView addSubview:miMaIcon];
    UIImage *miMaIconImg =[UIImage imageNamed:@"icon_password"];
    [miMaIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(miMaImgView).with.offset(16);
        make.centerY.equalTo(miMaImgView);
        make.width.offset(miMaIconImg.size.width);
        make.height.offset(miMaIconImg.size.height);
    }];
    miMaIcon.image = [UIImage imageNamed:@"icon_password"];
    
    UIView *miMaLine = [[UIView alloc] init];
    [miMaImgView addSubview:miMaLine];
    [miMaLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(miMaIcon.mas_right).with.offset(8);
        make.height.offset(14);
        make.width.offset(1);
        make.centerY.equalTo(miMaIcon);
        
    }];
    miMaLine.backgroundColor = getColor(@"ff40c6");
    
    self.miMaTF = [[UITextField alloc] init];
    [miMaImgView addSubview:self.miMaTF];
    [self.miMaTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(miMaIcon.mas_right).with.offset(17);
        make.right.equalTo(miMaImgView).with.offset(-16);
        make.centerY.equalTo(miMaIcon);
    }];
    self.miMaTF.placeholder = @"请输入密码";
    self.miMaTF.font = [UIFont systemFontOfSize:14];
    self.miMaTF.textColor = getColor(@"ff40c6");
    
    //
    //验证码
    UIImageView *yanZhengMaImgView = [[UIImageView alloc] init];
    [self.view addSubview:yanZhengMaImgView];
    [yanZhengMaImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(miMaImgView.mas_bottom).with.offset(16);
        make.left.equalTo(self.view).with.offset(38);
        make.right.equalTo(self.view).with.offset(-38);
        make.height.offset(44);
    }];
    
    yanZhengMaImgView.image = [UIImage imageNamed:@"line_frame"];
    yanZhengMaImgView.userInteractionEnabled = YES;
    
    UIImageView *yanZhengMaIcon = [[UIImageView alloc] init];
    [yanZhengMaImgView addSubview:yanZhengMaIcon];
    UIImage *yanZhengMaImg =[UIImage imageNamed:@"icon_Load"];
    [yanZhengMaIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(yanZhengMaImgView).with.offset(16);
        make.centerY.equalTo(yanZhengMaImgView);
        make.width.offset(yanZhengMaImg.size.width);
        make.height.offset(yanZhengMaImg.size.height);
    }];
    yanZhengMaIcon.image = [UIImage imageNamed:@"icon_Load"];
    
    UIView *yanZhengMaLine = [[UIView alloc] init];
    [yanZhengMaImgView addSubview:yanZhengMaLine];
    [yanZhengMaLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(yanZhengMaIcon.mas_right).with.offset(8);
        make.height.offset(14);
        make.width.offset(1);
        make.centerY.equalTo(yanZhengMaIcon);
        
    }];
    yanZhengMaLine.backgroundColor = getColor(@"ff40c6");
    
    self.yanZhengMaTF = [[UITextField alloc] init];
    [yanZhengMaImgView addSubview:self.yanZhengMaTF];
    [self.yanZhengMaTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(yanZhengMaIcon.mas_right).with.offset(17);
        make.centerY.equalTo(yanZhengMaIcon);
        make.width.offset(150);
    }];
    self.yanZhengMaTF.placeholder = @"请输入验证码";
    self.yanZhengMaTF.font = [UIFont systemFontOfSize:14];
    self.yanZhengMaTF.textColor = getColor(@"ff40c6");
    
    
    self.yzmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [yanZhengMaImgView addSubview:self.yzmBtn];
    [self.yzmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(yanZhengMaImgView);
        make.right.equalTo(yanZhengMaImgView);
        make.bottom.equalTo(yanZhengMaImgView);
        make.width.offset(100);
        
    }];
    
    [self.yzmBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
    [self.yzmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.yzmBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.yzmBtn setBackgroundImage:[UIImage imageNamed:@"b_bg2"] forState:UIControlStateNormal];
    [self.yzmBtn addTarget:self action:@selector(yanzhengma) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *phoneBtn = [[UIButton alloc] init];
    [self.view addSubview:phoneBtn];
    [phoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(38);
        make.right.equalTo(self.view).with.offset(-38);
        make.top.equalTo(yanZhengMaImgView.mas_bottom).with.offset(44);
        make.height.offset(44);
    }];
    
    [phoneBtn setBackgroundImage:[UIImage imageNamed:@"b_bg1"] forState:UIControlStateNormal];
    [phoneBtn setTitle:@"确定" forState:UIControlStateNormal];
    
    [phoneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    phoneBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    
    [phoneBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *xieYiLab = [[UILabel alloc] init];
    [self.view addSubview:xieYiLab];
    [xieYiLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).with.offset(-20);
    }];
    xieYiLab.font = [UIFont systemFontOfSize:12];
    xieYiLab.textColor = getColor(@"b8b8b8");
    xieYiLab.text = @"CopyRight ©️ weqrqwerqw";
    
}
- (void)yanzhengma{
    
    if (_zhangHaoTF.text.length == 0) {
        
        [PublicFunction showAlert:nil message:@"手机号不能为空" duration:1.0];
        return;
    }
    if (![TSRegularExpressionUtil validateMobile:_zhangHaoTF.text]) {
        
        [PublicFunction showAlert:nil message:@"请输入正确格式的手机号码" duration:1.0];
        return;
    }
    __block int timeout=60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    NSMutableDictionary *loginParams=[[NSMutableDictionary alloc]init];
    
    
    [loginParams setValue:self.zhangHaoTF.text forKey:@"mobile"];
    
    [[MNetworkHandler sharedInstance]conURL:smsSend
                                networkType:NetWorkPOST
                                     params:loginParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   
                                   if ([[returnData objectForKey:@"Status"]intValue] == 200) {
                                       
                                       
                                       
                                       [PublicFunction showAlert:nil message:@"验证码发送成功" duration:5.0f];
                                       
                                       
                                   }else{
                                       
                                       
                                       
                                       [PublicFunction showAlert:nil message:@"验证码发送失败" duration:1.0f];
                                   }
                                   
                               }
                               failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
    dispatch_source_set_event_handler(_timer, ^{
        
        if(timeout<=0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.yzmBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
                
                self.yzmBtn.userInteractionEnabled = YES;
            });
        }else{
            int seconds = timeout % 61;
            
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //NSLog(@"____%@",strTime);
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [self.yzmBtn setTitle:[NSString stringWithFormat:@"%@秒",strTime] forState:UIControlStateNormal];
                [UIView commitAnimations];
                self.yzmBtn.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
    
    
    
}

//////返回
- (void)leftNavItemClick{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}
- (void)sureClick{
    
    if (![TSRegularExpressionUtil validateMobile:_zhangHaoTF.text]) {
        
        [PublicFunction showAlert:nil message:@"请输入正确格式的手机号码" duration:1.0];
        return;
    }
    
    if (![TSRegularExpressionUtil validatePassword:_miMaTF.text]) {
        
        [PublicFunction showAlert:nil message:@"密码为长度6-16位大小写字母和数字" duration:1.0];
        return;
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: _zhangHaoTF.text forKey:@"mobile"];
    [productParams setValue: _miMaTF.text forKey:@"passwd"];
    [productParams setValue:_yanZhengMaTF.text forKey:@"smsCode"];
    [[MNetworkHandler sharedInstance]conURL:resetPass
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   if ([[returnData objectForKey:@"Status"]intValue] == 200) {
                                       
                                       [hud removeFromSuperview];
                                       
                                       [self dismissViewControllerAnimated:YES completion:^{
                                           
                                       }];
                                       
                                       
                                   }else{
                                       
                                       [hud removeFromSuperview];
//                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"Msg"] duration:1.0f];
                                   }
                                   
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:1.0f];
                               }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
